﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WinPhone.Models;

namespace WinPhone.Controllers
{
    public class LoginController : Controller
    {


        Repository repo = new Repository();

        // GET: /Login/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult AddGamer(string login, string password)
        {
            try
            {
                int id = repo.addGemer(login, password);
                return Json(id, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }

        }

        public ActionResult LogInGamer(string login, string password)
        {
            try
            {
                bool flagGamerInData = repo.InLoginSubmit(login);
                if (flagGamerInData)
                {
                    //авторизация успешна
                    return Json(true, JsonRequestBehavior.AllowGet);
                }
                else
                    return Json(false, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("Error300", JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult CreateNewGame(string login)
        {
            try
            {
                //add to database
                //Заполняем информацию об игроке, начавшем игру
                int game_id =repo.addGame(login, "White",   DateTime.UtcNow,"");
                return Json(game_id, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("Error300", JsonRequestBehavior.AllowGet);
            }
        }
        
        public ActionResult GetActualGames(string login)
        {
            try
            {
                //получаем все игры игрока которые он создал или имеет
                return Json(repo.getAllGame(login), JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("Error300", JsonRequestBehavior.AllowGet);
            }
        }

<<<<<<< HEAD
        public ActionResult ValidateSteps(string firstCell)
        {
            List<string> ValidCells = new List<string>();
            if (firstCell == "e2")
                ValidCells.Add("e4");
            if (firstCell == "d1")
                ValidCells.Add("h5");
            if (firstCell == "h5")
                ValidCells.Add("f7");
            if (firstCell == "f1")
                ValidCells.Add("c4");
            if (firstCell == "e7")
                ValidCells.Add("e5");
            if (firstCell == "b8")
                ValidCells.Add("c6");
            if (firstCell == "g8")
                ValidCells.Add("f6");
            return Json(ValidCells, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ViewLastStroke(int gameID, string login)
        {
            StrokeResult lastStroke = repo.getLastStroke(login, gameID);
            return Json(lastStroke, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Stroke(int gameID, string login, string CellFrom, string CellTo)
        {
            try
            {
                bool result = repo.addStroke(gameID, login, CellFrom, CellTo);
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("Error300", JsonRequestBehavior.AllowGet);
            }
        }
=======
        public ActionResult ValidSteps(string firstCell)
        {
            List<string> listCell=new List<string>();

            //Черные
            if (firstCell == "e7")
            {
                listCell.Add("e5");
            }
            if (firstCell == "b8")
            {
                listCell.Add("c6");
            }
            if (firstCell == "h7")
            {
                listCell.Add("h6");
            }



            return Json(listCell, JsonRequestBehavior.AllowGet);
        }


>>>>>>> 3d34fdd7ca1fba43a691c2b3ee1434d941663d56

        //закончить игру
        public ActionResult EndGame(int gameID, string login)
        {
            try
            {
                bool result = repo.deleteGame(gameID, login);
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("Error300", JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult SendChallenge(string login, int gameID)
        {
            try
            {
                bool result = repo.sendChallenge(login, gameID);
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("Error300", JsonRequestBehavior.AllowGet);
            }
        }
    }
}
